<?php

namespace App\Http\Controllers\Shaking;

use App\Contracts\Services\Storage\StorageService;
use App\Entities\Activity;
use App\Events\ShakingCreated;
use App\Events\ShakingUpdated;
use App\Http\Controllers\Activity\BaseController as BaseActivityController;
use Illuminate\Container\Container;
use Illuminate\Http\Request;
use Ramsey\Uuid\Uuid;

class ActivityController extends BaseActivityController
{
    public function show($id)
    {
        if (!Uuid::isValid($id)) {
            return response()->failed('id不合法');
        }

        $activity = $this->activities->findOne($id);

        if (null === $activity) {
            return response()->failed('没有找到资源');
        }

        $storage = Container::getInstance()->make(StorageService::class);

        $translated = [
            'id' => $activity->id,
            'category' => $activity->category,
            'name' => $activity->name,
            'start_time' => $activity->startTime,
            'end_time' => $activity->endTime,
            'limits' => \GuzzleHttp\json_decode($activity->limit),
            'rules' => \GuzzleHttp\json_decode($activity->rule),
            'background_img' => $storage->toHttpUrl($activity->backgroundImg),
            'background_color' => $activity->backgroundColor,
            'list_background_img' => $storage->toHttpUrl($activity->listBackgroundImg),
            'description' => $activity->description,
            'shares' => $this->formatShares($activity->shares),
            'app_url' => $activity->app_url,
        ];

        return response()->success($translated);
    }

    protected function parseRules(Request $request)
    {
        $rules = [
            'is_spot' => $request->json('rules.is_spot', false),
            'address' => $request->json('rules.address', ''),
            'longitude' => $request->json('rules.longitude', 0.0),
            'latitude' => $request->json('rules.latitude', 0.0),
            'range' => $request->json('rules.range', 1),
        ];

        return json_encode($rules);
    }

    protected function parseLimits(Request $request)
    {
        $limits = [
            'type' => $request->json('limits.type', Activity::LIMIT_DAY),
            'num' => $request->json('limits.num', 1),
        ];

        return json_encode($limits);
    }

    protected function getRulesForCreate()
    {
        return [
            'name' => 'required|max:255',
            'category' => 'required|in:1,2',
            'start_time' => 'required|date',
            'end_time' => 'required|date|after:start_time',
            'model_id' => 'required',
            'template_id' => 'required',
            'description' => 'min:1',
            'limits' => 'required|array',
            'limits.type' => 'in:day,all',
            'limits.num' => 'integer',
            'rules' => 'array',
            'rules.is_spot' => 'boolean',
            'rules.address' => 'required_if:rules.is_spot,1',
            'rules.longitude' => 'required_if:rules.is_spot,1|numeric',
            'rules.latitude' => 'required_if:rules.is_spot,1|numeric',
            'rules.range' => 'required_if:rules.is_spot,1|numeric',
            'shares' => 'array',
            'shares.title' => 'required_if:shares.isopen,on',
            'shares.content' => 'required_if:shares.isopen,on',
        ];
    }

    protected function getRulesForUpdate()
    {
        return [
            'name' => 'max:255',
            'category' => 'in:1,2',
            'start_time' => 'date',
            'end_time' => 'date|after:start_time',
            'description' => 'min:1',
            'limits' => 'array',
            'limits.type' => 'in:day,all',
            'limits.num' => 'integer',
            'rules' => 'array',
            'rules.is_spot' => 'boolean',
            // 'rules.address' => 'string',
            // 'rules.longitude' => 'numeric',
            // 'rules.latitude' => 'numeric',
            // 'rules.range' => 'numeric',
            'shares' => 'array',
            // 'shares.title' => 'string',
            // 'shares.content' => 'string',
        ];
    }

    protected function createdEvents(Activity $activity)
    {
        return event(new ShakingCreated($activity));
    }

    protected function updatedEvents(Activity $activity)
    {
        return event(new ShakingUpdated($activity));
    }
}
